package com.eventos;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.eventssmartphone.*;

public class EventoAdapter extends BaseAdapter{

	List<EventoBean> lista;
	Context context;
	LayoutInflater inflater;
	private final ImageDownloader imageDownloader = new ImageDownloader();
	
	public EventoAdapter(List<EventoBean> lista, Context context){
		this.lista = lista;
		this.context = context;
		inflater = LayoutInflater.from(context);
		
			
	}
 
	@Override
	public int getCount() {
		return lista.size();
	}

	@Override
	public Object getItem(int position) {
		return lista.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		
		if (convertView == null){
			convertView = inflater.inflate(R.layout.ev_itemlistaeventos, null);
			
			holder = new ViewHolder();
			holder.titEvento = (TextView) convertView.findViewById(R.id.titEvento);
			holder.fechaEvento = (TextView) convertView.findViewById(R.id.fechaEvento);
			holder.lugarEvento = (TextView) convertView.findViewById(R.id.lugarEvento);
			holder.imagenPublicitaria = (ImageView) convertView.findViewById(R.id.imgEvento);
			
			
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		
		EventoBean evento = lista.get(position);
		holder.titEvento.setText(evento.getTitEvento());
		holder.fechaEvento.setText(evento.getFechaEvento());
		holder.lugarEvento.setText(evento.getLugarEvento());
		//holder.imagenPublicitaria.setImageBitmap(downloadFile(evento.getUrlImagen()));
		imageDownloader.download(evento.getUrlImagen(), holder.imagenPublicitaria);
		return convertView;
		
	   
		
		
		
		
	}
	
	class ViewHolder{
		TextView titEvento;
		TextView fechaEvento;
		TextView lugarEvento;
		ImageView imagenPublicitaria;
	}

	
	
   /* Bitmap downloadFile(String fileUrl){
          URL myFileUrl =null;          
          try {
               myFileUrl= new URL(fileUrl);
          } catch (MalformedURLException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
               return null;
          }
          try {
               HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
               conn.setDoInput(true);
               conn.connect();
               InputStream is = conn.getInputStream();
               return BitmapFactory.decodeStream(is);
               
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
               return null;
          }
     }*/
	
	  public ImageDownloader getImageDownloader() {
	        return imageDownloader;
	    }
	
}
